require 'Object'


class Shot < MovableObject
  
  attr_accessor :turn_force, :move_speed
  attr_reader :owner
  attr_reader :time_to_live
  
  def initialize(x,y,angle, owner, image)
    shape = BaseShapes::Shot
    
    super(image, shape)
    
    @body.m = 1
    @body.i = CP::moment_for_poly(@body.m, shape, CP::Vec2.new(0,0))
    
    @body.p = CP::Vec2.new(x,y)
    @body.a = angle
    
    @shape.collision_type = :shot
    @owner = owner
    
    @type = :shot
  end
  
  def update
    @shape.body.v = (@shape.body.a.radians_to_vec2 * @move_speed)
    
    @time_to_live -= $dt
    
    if (@time_to_live < 0) then
      destroy
    end
  end
  
  def collided_with(other_object)
    if other_object == @owner then
      return false
    end
    
    # don't collide with shots belonging to the same owner
    if other_object.is_a? Shot then #and other_object.owner == @owner then
      return false
    end
    
    other_object.hit_by_weapon self, @type, @damage_value
    
    #todo: explosionation?
    #puts 'hit ' + other_object.to_s
    
    destroy
    return true
  end
  
end

class Bullet < Shot
  def initialize(x,y,angle,owner)
    super(x,y,angle,owner,Images[:shot])
    @damage_value = 1
    @move_speed = 100
    @time_to_live = 0.5 # seconds
    @collides_with_air = true
    
    @particle_system = ParticleEffect.new(Images[:shot], 30, 20, self)
    @particle_system.initial_scale = 0.75
    @particle_system.initial_alpha = 0.5
    @particle_system.randomization = 0
    @particle_system.particle_lifespan = 0.5
  end
  
  def destroy
    @particle_system.destroy
    super
  end
  
  def collided_with(ob)
    if super then
      #explode
    end
  end
end

class Rocket < Shot
  def initialize(x,y,angle,owner)
    super(x,y,angle,owner,Images[:rocket])
    @damage_value = 40
    @move_speed = 100
    @time_to_live = 2
    @collides_with_air = true
    
    @particle_system = ParticleEffect.new(Images[:smoke1], 30, 20, self)
    @particle_system.initial_scale=0.5
  end
  
  def destroy
    explode
    @particle_system.destroy
    super
  end
end

class LaserBolt < Shot
  def initialize(x,y,angle,owner)
    super(x,y,angle,owner,Images[:laser_blue])
    @damage_value = 5
    @move_speed = 300
    @time_to_live = 2
    @collides_with_air = false
  end
end

class Fire < Shot
  def initialize(x,y,angle,owner)
    super(x,y,angle,owner,Images[:shot])
    @damage_value = 5
    @move_speed = 300
    @time_to_live = 2
    @collides_with_air = false
    
    @particle_system = ParticleEffect.new(Images[:flames], 30, 20, self)
    @particle_system.initial_scale=0.1
    @particle_system.end_scale=1
  end
end
